//
//  addressInfoView.m
//  BeautyPicture
//
//  Created by 沈振邦 on 15/12/21.
//  Copyright © 2015年 szb. All rights reserved.
//

#import "addressInfoView.h"

@implementation addressInfoView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUpAddressView];
    }
    return self;
}

- (void)setUpAddressView
{
    CGFloat nameX = 15.0f;
    CGFloat nameY = 15.0f;
    CGFloat nameW = 120.0f;
    CGFloat nameH = 16.0f;
    _goodsName = [[UILabel alloc] initWithFrame:CGRectMake(nameX, nameY, nameW, nameH)];
    _goodsName.font = [UIFont systemFontOfSize:16.0f];
    [self addSubview:_goodsName];
    
    //2.联系电话
    CGFloat phoneX =CGRectGetMaxX(_goodsName.frame) + 10.0;
    CGFloat phoneY = _goodsName.y;
    CGFloat phoneH = nameH;
    CGFloat phoneW = 150.0f;
    _goodsPhone = [[UILabel alloc] initWithFrame:CGRectMake(phoneX, phoneY, phoneW, phoneH)];
    _goodsPhone.font = [UIFont systemFontOfSize:16.0f];
    [self addSubview:_goodsPhone];
    
    //3.收货详情
    CGFloat textX = nameX;
    CGFloat textY = CGRectGetMaxY(_goodsName.frame) + 5.0f;
    CGFloat textW = BPScreenW - nameX * 2.0;
    CGFloat textH = 40.0f;
    _goodsAddress = [[UILabel alloc] initWithFrame:CGRectMake(textX, textY, textW, textH)];
    _goodsAddress.textColor = [UIColor grayColor];
    _goodsAddress.lineBreakMode = NSLineBreakByCharWrapping;
    _goodsAddress.font = [UIFont systemFontOfSize:16.0f];
    _goodsAddress.numberOfLines = 0;
    [self addSubview:_goodsAddress];
    
    CGFloat lineY = CGRectGetMaxY(_goodsAddress.frame) + 10.0f;
    BPLineView *lineView = [[BPLineView alloc] initWithFrame:CGRectMake(10.0f, lineY - 1, BPScreenW - 20, 0.7)];
    [self addSubview:lineView];
    
    UIButton *chooseAdr = [UIButton buttonWithType:UIButtonTypeCustom];
//    CGFloat btnW = 80.0f;
//    CGFloat btnX = BPScreenW - BPStatusCellInset - btnW;
    CGFloat btnH = 23.0f;
//    CGFloat btnY = CGRectGetMaxY(lineView.frame) + 10.0f;
    
//    chooseAdr.frame = CGRectMake(btnX, btnY, btnW, btnH);
//    [chooseAdr setBackgroundColor:BPGlobaThemeColor];
//    [chooseAdr setTitle:@"其他地址" forState:UIControlStateNormal];
//    chooseAdr.titleLabel.font = [UIFont boldSystemFontOfSize:13.0f];
//    [chooseAdr.layer setMasksToBounds:YES];
//    [chooseAdr.layer setCornerRadius:3.0f];
//    [chooseAdr setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [chooseAdr addTarget:self action:@selector(chooseAddress) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:chooseAdr];
    
    //4.收货时间
    CGFloat timeX = nameX;
    CGFloat timeY = chooseAdr.y;
    CGFloat timeW = 200.0;
    CGFloat timeH = btnH;
    _goodsTime = [[UILabel alloc] initWithFrame:CGRectMake(timeX, timeY, timeW, timeH)];
    _goodsTime.font = [UIFont systemFontOfSize:16.0f];
    [self addSubview:_goodsTime];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(chooseAddress)];
    [self addGestureRecognizer:tap];
}

- (void)addData:(BPAddressModel *)addressModel
{
//    if (addressModel.province) {
//    [self withLabel:_goodsAddress addText:[NSString stringWithFormat:@"%@ %@ %@ %@",addressModel.province,addressModel.city,addressModel.area,addressModel.address]];
//    }
    _goodsAddress.text = [NSString stringWithFormat:@"%@%@%@",addressModel.province,addressModel.city,addressModel.address];
    _goodsName.text = addressModel.realname;
    _goodsTime.text = addressModel.time;
    _goodsPhone.text = addressModel.phone;
}

- (BOOL)checkAddress
{
    if (_goodsAddress.text == nil) {
        return NO;
    }
    if (_goodsPhone.text == nil) {
        return NO;
    }
    if (_goodsName.text == nil) {
        return NO;
    }
    return YES;
}

- (void)withLabel:(UILabel *)textLabel addText:(NSString *)text
{
    //创建NSMutableAttributedString实例，并将text传入
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:text];
    //创建NSMutableParagraphStyle实例
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
    //设置行距
    [style setLineSpacing:5.0f];
//    //判断内容长度是否大于Label内容宽度，如果不大于，则设置内容宽度为行宽（内容如果小于行宽，Label长度太短，如果Label有背景颜色，将影响布局效果）
    NSInteger leng = textLabel.width;
    if (attStr.length < textLabel.width) {
        leng = attStr.length;
    }
    //根据给定长度与style设置attStr式样
    [attStr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, leng)];
//    Label获取attStr式样
    textLabel.attributedText = attStr;
    //Label自适应大小
    [textLabel sizeToFit];
    //设置Label高度
    //return textLabel.frame.size.height;
}

- (void)chooseAddress
{
    if (self.delegate !=nil && [self.delegate respondsToSelector:@selector(chooseAnotherAddress)]) {
        [self.delegate chooseAnotherAddress];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
